package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据初始化监听器
 */
//@Component
public class RedisDataInitListener  implements ApplicationListener<ContextRefreshedEvent>{
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    //spring容器初始化之后, 马上执行的方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("-------------vo数据初始化---start-------------");
        //从mysql 查询所有攻略数据
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            StrategyStatisVO vo = new StrategyStatisVO();

            //如果redis中中已经存在, vo对象, 没必要进行初始化了
            if(strategyStatisRedisService.isExistVo(detail.getId())){
                continue;
            }
            BeanUtils.copyProperties(detail, vo);
            vo.setDestName(detail.getDest().getName());
            vo.setDestId(detail.getDest().getId());
            vo.setDetailId(detail.getId());
            strategyStatisRedisService.setStrategyStatisVo(vo);
        }
        //将攻略数据转换成vo对象即可
        System.out.println("-------------vo数据初始化----end------------");


        System.out.println("-------------zset排行数据初始---start-------------");
        //热门的攻略排行初始化: 浏览数 + 回复数
        for (StrategyDetail detail : list) {
            int score = detail.getViewnum() + detail.getReplynum();
            //思考: 是否需要判断redis中某个攻略对应的vo id已经初始化进入热门攻略排行中zset里面了?
            //如果zset中已经存在, 必须跳过, 防止数据覆盖
            //如果第一次  score = 100 分,那么 sid1 在zset中分数就是100, 后续页面执行相关操作
            //sid1 在zset分数变为了 200, 此时如果mgrsite进行维护, 又重启了, 如果没有执行跳过操作
            // 会将重复给sid 加了 100, 而真实的操作是不应该加
            if(strategyStatisRedisService.isValueInZset(detail.getId(), RedisKeys.STRATEGY_ZSET_HOT.getPrefix())){
                continue;
            }
            strategyStatisRedisService.addHotScore(detail.getId(), score);
        }
        //国内攻略排行初始化/国外的攻略排行初始化
        for (StrategyDetail detail : list) {
            int score = detail.getThumbsupnum() + detail.getFavornum();
            //满足在国内排行跟国外排行zset表中, 需要跳过
            if(strategyStatisRedisService.isValueInZset(detail.getId(), RedisKeys.STRATEGY_ZSET_CHINA.getPrefix())
                    || strategyStatisRedisService.isValueInZset(detail.getId(), RedisKeys.STRATEGY_ZSET_ABROAD.getPrefix())
                    ){
                continue;
            }
            strategyStatisRedisService.addCommendScore(detail.getId(), score);
        }
        System.out.println("-------------排行数据初始---end-------------");
    }
}
